package com.sinux.sshUser.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sinux.sshUser.dao.RoleDaoI;
import com.sinux.sshUser.dao.UserDaoI;
import com.sinux.sshUser.entity.Department;
import com.sinux.sshUser.entity.Role;
import com.sinux.sshUser.entity.User;
import com.sinux.sshUser.service.RoleServiceI;

@Service
public class RoleServiceImpl implements RoleServiceI {

	@Autowired
	private RoleDaoI roleDao;

	@Autowired
	private UserDaoI userDao;

	
	@Override
	public List<Role> findAllRoleList() {
		List<Role> roles=roleDao.find("from Role t");
		return roles;
	}
	
	@Override
	public List<Role> findRoleList(String value) {
		String hql="from Role u where (u.roid like '%"+value+"%' or u.roname like '%"+value+"%') ";
		List<Role> roles=roleDao.find(hql);
		return roles;
	}
	@Override
	public List<Role> findRoleList(String value,int page, int rows) {
		String hql="from Role u where (u.roid like '%"+value+"%' or u.roname like '%"+value+"%') ";
		List<Role> roles=roleDao.find(hql, page, rows);
		return roles;
	}
	
	@Override
	public List<Role> findRolePageing(int page, int rows) {
		String hql="from Role u ";
		List<Role> roles=roleDao.find(hql, page, rows);
		return roles;
	}

	@Override
	public void add(Role role) {
		// TODO Auto-generated method stub

	}

	@Override
	public Role get(String id) {
		String hql="from Role where roid="+id;
		return roleDao.get(hql);
	}

	@Override
	public List<User> findRoleUsers(String roid, int page, int rows) {
		String hql="SELECT a FROM User a LEFT JOIN a.roles b WHERE b.roid = "+roid+" order by a.uid ASC";
		List<User> users=userDao.find(hql, page, rows);
		return users;
	}

	@Override
	public void edit(Role role) {
		// TODO Auto-generated method stub

	}

	@Override
	public void delete(String id) {
		// TODO Auto-generated method stub

	}

	@Override
	public void grant(Role role) {
		// TODO Auto-generated method stub

	}
	
	@Override
	public boolean removeRoleUsers(String roid,String[] uids) {
		String hql="from Role d where d.roid="+roid;
		Role role=roleDao.get(hql);//获取部门
		Set<User> users=role.getUsers();//获取部门的用户
		List<User> list=new ArrayList<>();
		for(int i=0;i<uids.length;i++){
			//2.根据deid获取部门,
			User user=userDao.get("from User where uid="+uids[i]);//获取指定用户
			list.add(user);		
		}
		//3.移除用户
		boolean isTag=users.removeAll(list);//移除指定用户
		return isTag;
		
	}

}
